package com.example.jikeyoujikeyou.office_sz.view.activity.forget;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.model.HttpModel;
import com.example.jikeyoujikeyou.office_sz.service.INetCallBack;
import com.example.jikeyoujikeyou.office_sz.service.StaffService;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseFragment;

import java.util.logging.Logger;

public class VForgetPasswordFragment extends BaseFragment {

    private View activity_register_password;
    private Button btn_register_password_next;
    private EditText et_register_newPassword,et_register_enterPassword;
    private ImageView iv_show_new_password_eye,iv_show_enter_password_eye;

    public VForgetPasswordFragment() {}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity_register_password = inflater.inflate(R.layout.activity_forget_password,null,false);
        init();
        return activity_register_password;
    }

    private void init() {
        et_register_newPassword = activity_register_password.findViewById(R.id.et_register_newPassword);
        et_register_enterPassword = activity_register_password.findViewById(R.id.et_register_enterPassword);

        btn_register_password_next = activity_register_password.findViewById(R.id.btn_register_password_next);

        iv_show_new_password_eye = activity_register_password.findViewById(R.id.iv_show_new_password_eye);
        iv_show_enter_password_eye = activity_register_password.findViewById(R.id.iv_show_enter_password_eye);

        TextWatcher passwordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void afterTextChanged(Editable editable) {
                String et_register_newPassword_value = et_register_newPassword.getText().toString();
                String et_register_enterPassword_value = et_register_enterPassword.getText().toString();
                /*System.out.println("et_register_newPassword_value:" + et_register_newPassword_value);
                System.out.println("et_register_enterPassword_value:" + et_register_enterPassword_value);*/
                if (et_register_newPassword_value != null && et_register_enterPassword_value != null && et_register_newPassword_value.length() > 6 && et_register_enterPassword_value.length() == et_register_newPassword_value.length()) {
                    if (et_register_newPassword_value.equals(et_register_enterPassword_value)) {
                        Handler enableButton = new Handler() {
                            @Override
                            public void handleMessage(Message msg) {
                                super.handleMessage(msg);
                                btn_register_password_next.setEnabled(true);
                            }
                        };
                        enableButton.sendMessage(enableButton.obtainMessage());
                    }
                } else {
                    Handler enableButton = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            btn_register_password_next.setEnabled(false);
                        }
                    };
                    enableButton.sendMessage(enableButton.obtainMessage());
                }
            }
        };

        et_register_newPassword.addTextChangedListener(passwordWatcher);
        et_register_enterPassword.addTextChangedListener(passwordWatcher);

        iv_show_new_password_eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int type = et_register_newPassword.getInputType();
                if ( type != InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            et_register_newPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                            iv_show_new_password_eye.setSelected(true);
                        }
                    };
                    handler.sendMessage(handler.obtainMessage());
                } else {
                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            et_register_newPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            iv_show_new_password_eye.setSelected(false);
                        }
                    };
                    handler.sendMessage(handler.obtainMessage());
                }
            }
        });

        iv_show_enter_password_eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int type = et_register_enterPassword.getInputType();
                if ( type != InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            et_register_enterPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                            iv_show_enter_password_eye.setSelected(true);
                        }
                    };
                    handler.sendMessage(handler.obtainMessage());
                } else {
                    Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            et_register_enterPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                            iv_show_enter_password_eye.setSelected(false);
                        }
                    };
                    handler.sendMessage(handler.obtainMessage());
                }
            }
        });

        btn_register_password_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String new_password = et_register_newPassword.getText().toString();
                String enter_password = et_register_enterPassword.getText().toString();

                /*System.out.println("new_password:"+new_password);
                System.out.println("enter_password:"+enter_password);
                System.out.println("telephone:"+ ForgetActivity.telephone);*/

                if (!new_password.equals(enter_password)) {
                    new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("两次输入的密码不一致").setPositiveButton("确定", null).create().show();
                    return ;
                }

                boolean flag = false;

                StaffService.updatePassword(ForgetActivity.telephone, new_password , "1234", new INetCallBack() {
                    @Override
                    public void onRequstSuccess(HttpModel httpModel) {
                        Looper.prepare();
                        message(null);
                        Looper.loop();
                    }

                    @Override
                    public void onRequestFail(String msg) {
                        Looper.prepare();
                        new AlertDialog.Builder(getContext()).setTitle("提示").setMessage(msg).setPositiveButton("确定", null).create().show();
                        Looper.loop();
                    }

                    @Override
                    public void onRequestFail() {
                        Looper.prepare();
                        new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("请求失败，请检查网络连接。").setPositiveButton("确定", null).create().show();
                        Looper.loop();
                    }
                });
            }
        });
    }
}
